

#include "stdafx.h"
#include "pf_dimdiametric.h"
#include "pf_text.h"
#include "pf_solid.h"
#include "pf_graphic.h"
#include "pf_units.h"


/**
 * Constructor.
 *
 * @para parent Parent Entity Container.
 * @para d Common dimension geometrical data.
 * @para ed Extended geometrical data for diametric dimension.
 */
PF_DimDiametric::PF_DimDiametric(PF_Container* parent,
                           const PF_DimensionAttribute& d,
                           const PF_DimDiametricAttribute& ed)
        : PF_Dimension(parent, d), edata(ed) {

    calculateBorders();
}



/**
 * @return Automatically created label for the default 
 * measurement of this dimension.
 */
QString PF_DimDiametric::getMeasuredLabel() {

    // Definitive dimension line:
    double dist = data.definitionPoint.distanceTo(edata.definitionPoint);

	PF_Graphic* graphic = getGraphic();

    QString ret;
	if (graphic!=NULL) {
		ret = PF_Units::formatLinear(dist, graphic->getUnit(), 
			graphic->getLinearFormat(), graphic->getLinearPrecision());
	}
	else {
        ret = QString("%1").arg(dist);
	}

    return ret;
}



PF_VectorSolutions PF_DimDiametric::getRefPoints() {
	PF_VectorSolutions ret(edata.definitionPoint,
						data.definitionPoint, data.middleOfText);
	return ret;
}


/**
 * Updates the sub entities of this dimension. Called when the 
 * dimension or the position, alignment, .. changes.
 *
 * @param autoText Automatically reposition the text label
 */
void PF_DimDiametric::update(bool autoText) {

    PF_DEBUG->print("PF_DimDiametric::update");

    clear();

	if (isUndone()) {
		return;
	}

    // dimension line:
    updateCreateDimensionLine(data.definitionPoint, edata.definitionPoint,
	true, true, autoText);

    calculateBorders();
}



void PF_DimDiametric::move(PF_Vector offset) {
    PF_Dimension::move(offset);

    edata.definitionPoint.move(offset);
    update();
}



void PF_DimDiametric::rotate(PF_Vector center, double angle) {
    PF_Dimension::rotate(center, angle);

    edata.definitionPoint.rotate(center, angle);
    update();
}



void PF_DimDiametric::scale(PF_Vector center, PF_Vector factor) {
    PF_Dimension::scale(center, factor);

    edata.definitionPoint.scale(center, factor);
	edata.leader*=factor.x;
    update();
}



void PF_DimDiametric::mirror(PF_Vector axisPoint1, PF_Vector axisPoint2) {
    PF_Dimension::mirror(axisPoint1, axisPoint2);

    edata.definitionPoint.mirror(axisPoint1, axisPoint2);
    update();
}



void PF_DimDiametric::moveRef(const PF_Vector& ref, const PF_Vector& offset) {

    if (ref.distanceTo(edata.definitionPoint)<1.0e-4) {
		PF_Vector c = (edata.definitionPoint + data.definitionPoint)/2.0;
		double d = c.distanceTo(edata.definitionPoint);
		double a = c.angleTo(edata.definitionPoint + offset);
		
		PF_Vector v;
		v.setPolar(d, a);
        edata.definitionPoint = c + v;
		data.definitionPoint = c - v;
		update(true);
    }
    else if (ref.distanceTo(data.definitionPoint)<1.0e-4) {
		PF_Vector c = (edata.definitionPoint + data.definitionPoint)/2.0;
		double d = c.distanceTo(data.definitionPoint);
		double a = c.angleTo(data.definitionPoint + offset);
		
		PF_Vector v;
		v.setPolar(d, a);
        data.definitionPoint = c + v;
		edata.definitionPoint = c - v;
		update(true);
    }
	else if (ref.distanceTo(data.middleOfText)<1.0e-4) {
        data.middleOfText.move(offset);
		update(false);
    }
}



/**
 * Dumps the point's data to stdout.
 */
std::ostream& operator << (std::ostream& os, const PF_DimDiametric& d) {
    os << " DimDiametric: " << d.getData() << "\n" << d.getEData() << "\n";
    return os;
}

